﻿using System;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using System.Windows.Forms;

namespace Petaframe.GenerationExtension
{
    /// <summary>
    /// This is the class that implements the package exposed by this assembly.
    ///
    /// The minimum requirement for a class to be considered a valid package for Visual Studio
    /// is to implement the IVsPackage interface and register itself with the shell.
    /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
    /// to do it: it derives from the Package class that provides the implementation of the 
    /// IVsPackage interface and uses the registration attributes defined in the framework to 
    /// register itself and its components with the shell.
    /// </summary>
    // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
    // a package.
    [PackageRegistration(UseManagedResourcesOnly = true)]
    // This attribute is used to register the informations needed to show the this package
    // in the Help/About dialog of Visual Studio.
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    // This attribute is needed to let the shell know that this package exposes some menus.
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [Guid(GuidList.guidPetaframe_GenerationExtensionPkgString)]
    public sealed class Petaframe_GenerationExtensionPackage : Package
    {
        DTE dte;

        /// <summary>
        /// Default constructor of the package.
        /// Inside this method you can place any initialization code that does not require 
        /// any Visual Studio service because at this point the package object is created but 
        /// not sited yet inside Visual Studio environment. The place to do all the other 
        /// initialization is the Initialize method.
        /// </summary>
        public Petaframe_GenerationExtensionPackage()
        {
            //Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
        }

        /////////////////////////////////////////////////////////////////////////////
        // Overriden Package Implementation
        #region Package Members

        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initilaization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (null != mcs)
            {
                // Create BS-DAL Project command for the menu item.
                CommandID bsProjCommandID = new CommandID(GuidList.guidPetaframe_ProjectGenerationExtensionCmdSet, (int)PkgCmdIDList.bsProjectGeneratorCommand);
                MenuCommand bsProjMenuItem = new MenuCommand(BsProjMenuItemCallback, bsProjCommandID);
                mcs.AddCommand(bsProjMenuItem);

                CommandID dalProjCommandID = new CommandID(GuidList.guidPetaframe_ProjectGenerationExtensionCmdSet, (int)PkgCmdIDList.dalProjectGeneratorCommand);
                MenuCommand dalProjMenuItem = new MenuCommand(DalProjMenuItemCallback, dalProjCommandID);
                mcs.AddCommand(dalProjMenuItem);

                // Create BS-DAL Project Item command for the menu item.
                CommandID bsProjItemCommandID = new CommandID(GuidList.guidPetaframe_ProjectItemGenerationExtensionCmdSet, (int)PkgCmdIDList.bsProjectItemGeneratorCommand);
                MenuCommand bsProjItemMenuItem = new MenuCommand(BsProjItemMenuItemCallback, bsProjItemCommandID);
                mcs.AddCommand(bsProjItemMenuItem);

                CommandID dalProjItemCommandID = new CommandID(GuidList.guidPetaframe_ProjectItemGenerationExtensionCmdSet, (int)PkgCmdIDList.dalProjectItemGeneratorCommand);
                MenuCommand dalProjItemMenuItem = new MenuCommand(DalProjItemMenuItemCallback, dalProjItemCommandID);
                mcs.AddCommand(dalProjItemMenuItem);

                dte = GetService(typeof(DTE)) as DTE;
            }
        }
        #endregion

        #region CREATE PROJECT 

        private void BsProjMenuItemCallback(object sender, EventArgs e)
        {
            if (dte.SelectedItems.MultiSelect)
                return;

            //SelectedItem item = dte.SelectedItems.Item(0);
            

            BSGeneratorForm bsForm = new BSGeneratorForm();
            bsForm.IsProjectCreated = true;
            DialogResult dr = bsForm.ShowDialog();

            if (dr == DialogResult.Abort)
                return;

            Solution solution = dte.Solution;
            solution.AddFromFile(bsForm.ProjectFileFullName);
            solution.SaveAs(solution.FullName);
        }

        private void DalProjMenuItemCallback(object sender, EventArgs e)
        {
            if (dte.SelectedItems.MultiSelect)
                return;

            DataTypeGeneratorForm dalForm = new DataTypeGeneratorForm();
            dalForm.IsProjectCreated = true;
            DialogResult dr = dalForm.ShowDialog();

            if (dr == DialogResult.Abort)
                return;

            Solution solution = dte.Solution;
            solution.AddFromFile(dalForm.ProjectFileFullName);
            solution.SaveAs(solution.FullName);
        }

        #endregion

        #region CREATE PROJECT ITEM

        private void BsProjItemMenuItemCallback(object sender, EventArgs e)
        {
            if (dte.SelectedItems.MultiSelect)
                return;

            BSGeneratorForm bsForm = new BSGeneratorForm();
            bsForm.IsProjectCreated = false;
            DialogResult dr = bsForm.ShowDialog();

            if (dr == DialogResult.Abort)
                return;
        }

        private void DalProjItemMenuItemCallback(object sender, EventArgs e)
        {
            if (dte.SelectedItems.MultiSelect)
                return;

            DataTypeGeneratorForm dalForm = new DataTypeGeneratorForm();
            dalForm.IsProjectCreated = false;
            DialogResult dr = dalForm.ShowDialog();

            if (dr == DialogResult.Abort)
                return;
            
            //dte.ActiveSolutionProjects[0]
        }

        #endregion

        //IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
            //Guid clsid = Guid.Empty;
            //int result;
            //Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(
            //           0,
            //           ref clsid,
            //           "VSPackage1",
            //           string.Format(CultureInfo.CurrentCulture, solution.FullName, this.ToString()),
            //           string.Empty,
            //           0,
            //           OLEMSGBUTTON.OLEMSGBUTTON_OK,
            //           OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
            //           OLEMSGICON.OLEMSGICON_INFO,
            //           0,        // false
            //           out result));

    }
}
